package com.sixteen.theGreat.settings.web.controller;

import com.sixteen.theGreat.commons.constants.Constants;
import com.sixteen.theGreat.commons.utils.DateUtil;
import com.sixteen.theGreat.commons.utils.UUIDUtil;
import com.sixteen.theGreat.settings.entity.User;
import com.sixteen.theGreat.settings.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class UserController {
    @Autowired
    private UserService userService;
    /**
     * 跳转到登录页面
     * @return
     */
    @GetMapping("/settings/qx/user/toLogin.do")
    public String toLogin(){
        return "settings/qx/user/login";
    }

    /**
     * 验证登录信息
     * @param userName 用户输入的用户名
     * @param password 用户输入的密码
     * @return 将结果信息封装到一个实体类中,返回该实体类
     */
    @PostMapping("/settings/qx/user/verify.do")
    @ResponseBody
    public Object verify(String userName,String password,HttpServletRequest request){
        return userService.queryUser(userName,password,request);
    }

    /**
     * 跳转到注册页面
     * @return
     */
    @GetMapping("/settings/qx/user/toRegister.do")
    public String toRegister(){
        return "settings/qx/user/register";
    }

    /**
     * 注册新用户
     * @return 返回的包装了注册成功与否的信息的实体类
     */
    @ResponseBody
    @PostMapping("/settings/qx/user/register.do")
    public Object register(String userName,String password,HttpServletRequest request){
        User user= User.builder()
                .userId(UUIDUtil.getUUID())
                .userName(userName)
                .password(password)
                .timeRegister(DateUtil.getCurrentDateTime()).build();
        return userService.addUser(user,request);
    }

    /**
     * 实现登出功能,并跳转到登录页面
     * @return
     */
    @GetMapping("/settings/qx/user/logout.do")
    public String logout(HttpServletRequest request){
        request.getSession().invalidate();
        return "redirect:/";
    }
}
